/**
 * Copyright 2010 Jake Jackson
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.jake.nb.ext.ant.hints.impl.dependserrors;

import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
import javax.swing.text.Document;
import org.jake.nb.ext.ant.parser.DocumentUtilities;
import org.jake.nb.ext.ant.parser.Span;
import org.netbeans.spi.editor.hints.ChangeInfo;
import org.netbeans.spi.editor.hints.Fix;

class RemoveInvalidEntriesForDependsFix implements Fix {

  private final Span span;
  private final Document document;
  private final List<String> toRemove;
  private final String text;

  /**
   * Creates a new Fix to remove invalid entries in depends entry.
   * @param document the document to remove in
   * @param span the depends span
   * @param toRemove a list of entries to remove. if a string is present more than one time, it has to be present in
   * this list as often as it should be removed
   * @param dependsValue the full string representing the depends values (means all after the first quote character)
   * @param textToDisplay the text to display as hint for the fix
   */
  RemoveInvalidEntriesForDependsFix(final Document document, final Span span, final List<String> toRemove, final String textToDisplay) {
    this.document = document;
    this.span = span;
    this.toRemove = toRemove;
    this.text = textToDisplay;
  }

  @Override
  public String getText() {
    return text;
  }

  @Override
  public ChangeInfo implement() throws Exception {
    final ChangeInfo info = new ChangeInfo();
    String fullText = document.getText(span.getStart(), span.getLength() + 1);
    for (final String string : toRemove) {
      final String remove = string.trim();
      fullText = fullText.replace(remove, ""); // NOI18N
    }

    // create the new list
    final List<String> usableTokens = new ArrayList<String>();
    final StringTokenizer tokenizer = new StringTokenizer(fullText, ","); // NOI18N
    while (tokenizer.hasMoreTokens()) {
      final String nextToken = tokenizer.nextToken();
      final String trimmed = nextToken.trim().replaceAll("\"", ""); // NOI18N

      // may the next token is an empty string (caused by replacement)
      if(trimmed.length() > 0) {
        usableTokens.add(nextToken);
      }
    }

    final StringBuilder builder = new StringBuilder();
    for (int i = 0; i < usableTokens.size(); i++) {
      final String string = usableTokens.get(i);
      builder.append(string);
      if(i < usableTokens.size() - 1) {
        builder.append(','); // NOI18N
      }
    }
    
    if(!builder.toString().endsWith("\"")) { // NOI18N
      builder.append('"'); // NOI18N
    }

    document.remove(span.getStart(), span.getLength() + 1);
    document.insertString(span.getStart(), builder.toString(), null);
    DocumentUtilities.save(document);
    return info;
  }
}
